Page({
    data: {
        timer: null,
        num:1,
        openid:'',
        access_token: '',
        txtOrderCode: ''
    },
    onLoad: function(options) {
        var that = this
        wx.login({
            success: function(data) {
                // 获取openid
                wx.request({
                    url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wxe45e229c6aa8c025&secret=222a4835ef6593d4bc6d3e103c4b8745&js_code=' + data.code + '&grant_type=authorization_code',
                    data: {},
                    header: {
                        'content-type': 'application/json'
                    },
                    success: function (res) {
                        that.setData({
                            openid: res.data.openid
                        })
                        wx.request({
                            url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxe45e229c6aa8c025&secret=222a4835ef6593d4bc6d3e103c4b8745',
                            method: "GET",
                            success: function(res) {
                                console.log(res)
                                that.setData({
                                    access_token: res.data.access_token,
                                })
                            }
                        })
                    }
                })

            }
        })
        // // 获取access_token
        // wx.request({
        //     url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + that.data.openid + '&secret=' + that.data.session_key,
        //     method: "GET",
        //     success: function(res) {
        //         console.log(res)
        //         // that.setData({
        //         //     access_token: res.data.access_token,
        //         // })
        //     }
        // })
    },

    // 点击执行方法
    form: function(e) {
        var that = this;
        var fId = e.detail.formId;
        // 网络请求
        var l = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + that.data.access_token;
        // 需要传的参数
        var d = {
            touser: that.data.openid, //用户的openid
            template_id: 'YrXHTrpJuDJcpHdXiG77bJmgKtIKFBsb0-OvQypYns0', //这个是申请的模板消息id，位置在微信公众平台/模板消息中添加并获取
            page: '/pages/index/index', //点击通知跳转的页面
            form_id: fId, //表单提交场景下，为 submit 事件带上的 formId

            //此处必须为data,只有人说value也可以,可能官方已经修复这个bug
            data: {
                "keyword1": {
                    "value": "入职考试",
                    "color": "#4a4a4a"
                },
                "keyword2": {
                    "value": "16:10~18:10",
                    "color": "#9b9b9b",
                },
                "keyword3": {
                    "value": "2018-06-28",
                    "color": "#9b9b9b"
                },
                "keyword4": {
                    "value": "请提前登陆教务处网站确认考试信息",
                    "color": "#9b9b9b"
                },
                "keyword5": {
                    "value": "本次考试主要内容为高等数学有关内容",
                    "color": "#9b9b9b"
                },
                "keyword6": {
                    "value": "开卷考试",
                    "color": "#9b9b9b"
                },
                "keyword7": {
                    "value": "2016-12-28",
                    "color": "#9b9b9b"
                },
            },
            color: '#ccc',
            emphasis_keyword: 'keyword1.DATA'
        }
        wx.request({
            url: l,
            data: d,
            method: 'POST', //此处不能有请求头
            success: function(res) {
                console.log(res, "push msg");
            },
            fail: function(err) {
                console.log(err, "push err");
            }
        });
    },
    pay: function () {
        var ordercode = this.data.txtOrderCode;
        wx.login({
            success: function (res) {
                if (res.code) {
                    wx.request({
                        url: 'https://www.yourdomain.com/pay',
                        data: {
                            code: res.code,//要去换取openid的登录凭证
                            ordercode: ordercode
                        },
                        method: 'GET',
                        success: function (res) {
                            console.log(res.data)
                            wx.requestPayment({
                                timeStamp: res.data.timeStamp,
                                nonceStr: res.data.nonceStr,
                                package: res.data.package,
                                signType: 'MD5',
                                paySign: res.data.paySign,
                                success: function (res) {
                                    // success
                                    console.log(res);
                                },
                                fail: function (res) {
                                    // fail
                                    console.log(res);
                                },
                                complete: function (res) {
                                    // complete
                                    console.log(res);
                                }
                            })
                        }
                    })
                } else {
                    console.log('获取用户登录态失败！' + res.errMsg)
                }
            }
        });
    },
    getOrderCode: function (event) {
        this.setData({
            txtOrderCode: event.detail.value
        });
    },
    startBtn: function () {
        console.log("开始按钮");
        this.Countdown();
    },

    　　// 自定义的暂停按钮  
    pauseBtn: function () {
        console.log("暂停按钮");
        clearTimeout(this.data.timer);
    },
    Countdown() {
        var _this = this;
        var timer = setTimeout(function () {
            var num = _this.data.num;
            num++
            console.log(num)
            _this.Countdown();
            _this.setData({
                num: num
            })
        }, 1000);
        _this.setData({
            timer: timer,
        })
    }
})